﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Perpus.ServiceReference1;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;

namespace Perpus
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }


        string koneksi = MappingDatabase.koneksi;

        public string idLogin;

        public string _textBoxUsername
        {
            get { return idLogin; }
        }

        private ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(koneksi))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<AnggotaMap>())
                .BuildSessionFactory();
        }

        private void LoginBtn_Click(object sender, EventArgs e)
        {
            Service1Client client = new Service1Client();
            Employee emp = new Employee();
            emp.Username_Pegawai = username_textBox1.Text;
            emp.Password_Pegawai = password_textBox2.Text;
            
            client.Open();

            var factory = CreateSessionFactory();
            try
            {
                using (var session = factory.OpenSession())
                {
                    string user = username_textBox1.Text;
                    var idPeg = from c in session.Query<Pegawai>()
                                 where c.username == user
                                 select c.id_pegawai;
                    int temp = idPeg.First();
                    var userPeg = from c in session.Query<Pegawai>()
                                  where c.id_pegawai == temp
                                  select c.nama_pegawai;
                    idLogin = userPeg.First();
                }
                if (username_textBox1.Text != "" && password_textBox2.Text != "")
                {
                    bool a = client.ceklogin(emp);
                    if (a == true)
                    {
                        MessageBox.Show("Selamat Datang!");
                        admin_MenuAwal menu = new admin_MenuAwal();
                        menu.Show();
                        menu._labelWelcome = _textBoxUsername;
                        this.Hide();
                        client.Close();
                    }
                    else
                    {
                        MessageBox.Show("Password Salah!");
                    }

                }
                else
                {
                    MessageBox.Show("Tidak Boleh Kosong!");
                }
                     
            }
            catch (QueryException ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {

        }
    }
}
